#!/usr/bin/env python3
"""
测试运行脚本

使用方法：
    python3 run_tests.py                    # 运行所有测试
    python3 run_tests.py -v                 # 详细输出
    python3 run_tests.py TestSharedMemoryComm  # 运行特定测试类
    python3 run_tests.py -k test_send       # 运行包含特定关键词的测试
"""

import unittest
import sys
import os

# 添加项目根目录到Python路径
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

def run_tests():
    """运行所有测试"""
    # 发现并运行测试
    loader = unittest.TestLoader()
    start_dir = os.path.dirname(os.path.abspath(__file__))
    suite = loader.discover(start_dir, pattern='test_*.py')
    
    # 运行测试
    runner = unittest.TextTestRunner(verbosity=2)
    result = runner.run(suite)
    
    # 返回测试结果
    return result.wasSuccessful()

def run_simple_tests():
    """运行简化的通信测试"""
    from test_simple_communication import TestSharedMemoryCommSimple, TestCommunicationFlow, TestMessageDataIntegrity
    
    # 创建测试套件
    suite = unittest.TestSuite()
    
    # 添加测试类
    suite.addTest(unittest.makeSuite(TestSharedMemoryCommSimple))
    suite.addTest(unittest.makeSuite(TestCommunicationFlow))
    suite.addTest(unittest.makeSuite(TestMessageDataIntegrity))
    
    # 运行测试
    runner = unittest.TextTestRunner(verbosity=2)
    result = runner.run(suite)
    
    return result.wasSuccessful()

if __name__ == '__main__':
    success = run_tests()
    sys.exit(0 if success else 1)
